<?php


namespace App\Http\Services\Admin\Account;


use App\Http\Constants\Account\CommonCoinCoinConstants;
use App\Http\Constants\Account\CommonCoinCoinLogConstants;
use App\Http\Constants\Account\CommonCoinReserveCoinLogConstants;
use App\Http\Services\Admin\CommonService;
use App\Models\CommonCoinCoin;
use App\Models\CommonCoinCoinLog;
use YiluTech\ComplexSearch\ComplexSearch;

class CommonCoinCoinLogService extends ComplexSearch
{
    public $root = CommonCoinCoinLog::class;
    protected $orderBy = 'id desc';
    protected $display = 'simple';

    protected $memberid = null;
    protected $username = null;
    protected $accountType = null;
    protected $typeName = null;
    protected $coin = null;


    protected $headers = [
        ['label' => '红包账户ID', 'value' => 'id'],
        ['label' => '会员ID', 'value' => 'member_id'],
        ['label' => '红包账户类型', 'value' => 'account_type'],
        ['label' => '会员昵称', 'value' => 'nickname'],
        ['label' => '会员手机号', 'value' => 'username'],
        ['label' => '红包明细', 'value' => 'child_type'],
        ['label' => '红包变动', 'value' => 'coin'],
        ['label' => '备注', 'value' => 'remark'],
        ['label' => '订单编号', 'value' => 'order_no'],
        ['label' => '创建时间', 'value' => 'create_time'],
    ];
    protected $conditions = [
        'order_no' => [
            'label' => '订单编号',
            'ctype' => 'input',
            'operator' => 'like',
        ],
        'member_id' => [
            'label' => '会员ID',
            'ctype' => 'input',
            'operator' => '=',
            'custom' => true,
        ],
        'username' => [
            'label' => '会员手机号',
            'ctype' => 'input',
            'operator' => '=',
            'custom' => true,
        ],
        'coin_id' => [
            'label' => '红包账户ID',
            'ctype' => 'input',
            'operator' => '=',
        ],
        'account_type' => [
            'label' => '账户类型',
            'ctype' => 'select',
            'operator' => CommonCoinCoinConstants::COINS_ACCOUNT_TYPE,
            'custom' => true,
        ],
        'type_name' => [
            'label' => '红包明细',
            'ctype' => 'input',
            'operator' => 'like',
            'custom' => true,
        ],
        'coin' => [
            'label' => '红包变动',
            'ctype' => 'select',
            'operator' => CommonCoinCoinLogConstants::COIN_ACTION,
        ],
        'create_time' => [
            'label' => '创建时间',
            'ctype' => 'date-in',
            'format' => 'Y-m-d H:i',
            'display' => true,
        ],
    ];

    public function make($request)
    {
        if ($request['action'] !== 'prepare') {
            if (!empty($request['params'])) {
                foreach ($request['params'] as $key => $value) {
                    if ($value[0] == "member_id") {
                        $this->memberid = $value[2];
                        unset($request['params'][$key]);
                    }
                    if ($value[0] == "username") {
                        $this->username = $value[2];
                        unset($request['params'][$key]);
                    }
                    if ($value[0] == "account_type") {
                        $this->accountType = $value[2];
                        unset($request['params'][$key]);
                    }
                    if ($value[0] == "type_name") {
                        $this->typeName = $value[2];
                        unset($request['params'][$key]);
                    }
                    if ($value[0] == 'coin') {
                        $this->coin = $value[2];
                        unset($request['params'][$key]);
                    }

                }
            }
        }
        return parent::make($request);

    }


    public function query()
    {
        $select = ['id', 'coin_id', 'child_type', 'coin', 'remark', 'create_time', 'now_coin', 'order_no'];
        $query = parent::query();
        $query->select($select);
        $query->with('CommonCoinCoin:id,member_id,account_type');

        if (!is_null($this->memberid)) {
            $userInfo = \SharedCache::getMember($this->memberid);
            if (!empty($userInfo['coin_id'])) {
                $query->where('coin_id', $userInfo['coin_id']);
            }
        }
        if (!is_null($this->username)) {
            $userInfo = \SharedCache::memberInfoByPhone($this->username);
            if (!empty($userInfo['coin_id'])) {
                $query->where('coin_id', $userInfo['coin_id']);
            }
        }
        if (!is_null($this->accountType)) {
            $query->whereHas('CommonCoinCoin' , function ($query) {
                $query->where('account_type', $this->accountType);
            });
        }

        if (!is_null($this->typeName)) {
            $childTypeData = array_column(CommonCoinReserveCoinLogConstants::COMMONCOIN_TYPE, 'label', 'value');
            $childTypeFilterData = array_filter($childTypeData, function ($v) {
                if (strpos($v, $this->typeName) !== false) return $v;
            });
            $inMap = array_keys($childTypeFilterData);
            if (!empty($inMap)) {
                $query->whereIn('child_type', $inMap);
            }
        }

        if (!is_null($this->coin) && !empty($this->coin)) {
            $operator = ($this->coin) > 0 ? '>' : '<';
            $query->where('coin', $operator, 0);
        }


        return $query;
    }

    public function get()
    {
        $result = parent::get();
        if ($this->action !== 'prepare') {
            $result = $result->toArray();
            if (!empty($result['data'])) {
                $accountTypeData = array_column(CommonCoinCoinConstants::COINS_ACCOUNT_TYPE, 'label', 'value');
                foreach ($result['data'] as $k => &$v) {
                    $v['account_type'] = $v['common_coin_coin']['account_type'];
                    $v['account_type_name'] = array_key_exists($v['account_type'], $accountTypeData) ? $accountTypeData[$v['account_type']] : null;
                    $v['member_id'] = $v['common_coin_coin']['member_id'];
                    unset($v['common_coin_coin']);
                    $member = \SharedCache::getMember($v['member_id']);
                    $v['nickname'] = $member['nickname'] ?: '';
                    $v['username'] = $member['username'] ?: '';
                }
            }
            $result = CommonService::objRegroupArr($result);
        }
        return $result;
    }

    public function detail($id)
    {
        $result = CommonCoinCoinLog::with('CommonCoinCoin:id,member_id,account_type')
            ->find($id);
        $result = $result->toArray();
        $member = \SharedCache::getMember($result['common_coin_coin']['member_id']);
        $result['nickname'] = $member['nickname'] ?: '';
        $result['username'] = $member['username'] ?: '';
        unset($result['common_coin_coin']);
        return $result;
    }


}
